package com.example.accounting.slice;

import com.example.accounting.ResourceTable;
import com.example.accounting.model.AccountInfo;
import com.example.accounting.model.AccountOperate;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.window.dialog.CommonDialog;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StatisticsSlice extends AbilitySlice {
    private static final int DIALOG_WIDTH = 900;

    private static final int DIALOG_HEIGHT = 900;

    private static final int DIALOG_CORNER_RADIUS = 30;

    private String selectTextVal;
    private Text statistic_showYear;
    private Text statistic_showMonth;

    private DirectionalLayout directionalLayout;
    private Image image;

    private String initListVal;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_statistics);

        initComponent();
        setImage();
        initType();
        initList("all");
    }

    private void initComponent() {
        image = (Image) findComponentById(ResourceTable.Id_statistic_arrow);

        statistic_showYear = (Text) findComponentById(ResourceTable.Id_statistic_showYear);
        statistic_showMonth = (Text) findComponentById(ResourceTable.Id_statistic_showMonth);

        directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_show_year_month);
        directionalLayout.setClickedListener(component -> showContactDetailDialog());

        statistic_showYear.setVisibility(Component.HIDE);
        statistic_showMonth.setVisibility(Component.HIDE);
        image.setVisibility(Component.INVISIBLE);
    }

    private void showContactDetailDialog() {
        Component container = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_select_year_month, null, false);

        DatePicker datePicker = (DatePicker) container.findComponentById(ResourceTable.Id_data_picker);
        if (selectTextVal.equals("月")) {
            datePicker.setDateOrder(DatePicker.DateOrder.YM);
        } else if (selectTextVal.equals("年")) {
            datePicker.setDateOrder(DatePicker.DateOrder.YEAR);
        }

        Button button_yes = (Button) container.findComponentById(ResourceTable.Id_select_yes);
        Button button_no = (Button) container.findComponentById(ResourceTable.Id_select_no);

        CommonDialog commonDialog = new CommonDialog(this);
        commonDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
        commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
        commonDialog.setContentCustomComponent(container);
        commonDialog.show();

        button_yes.setClickedListener(component -> {
            statistic_showYear.setText(datePicker.getYear() + "年");
            statistic_showMonth.setText(datePicker.getMonth() + "月");
            initList(initListVal);
            commonDialog.hide();
        });

        button_no.setClickedListener(component -> commonDialog.hide());
    }

    private void initType() {
        Text selectMonth = (Text) findComponentById(ResourceTable.Id_statistics_month);
        Text selectYear = (Text) findComponentById(ResourceTable.Id_statistics_year);
        Text selectAll = (Text) findComponentById(ResourceTable.Id_statistics_all);

        Element element = null;
        if (selectAll.getBackgroundElement() != null) {
            element = selectAll.getBackgroundElement();

        } else if (selectYear.getBackgroundElement() != null) {
            element = selectYear.getBackgroundElement();
        }
        if (selectMonth.getBackgroundElement() != null) {
            element = selectMonth.getBackgroundElement();
        }
        selectTextVal = "月";
        selectMonth.setBackground(null);
        selectYear.setBackground(null);
        selectAll.setBackground(element);


        Element finalElement = element;
        selectMonth.setClickedListener(component -> {
            statistic_showYear.setVisibility(Component.VISIBLE);
            statistic_showMonth.setVisibility(Component.VISIBLE);
            image.setVisibility(Component.VISIBLE);
            selectMonth.setBackground(finalElement);
            selectYear.setBackground(null);
            selectAll.setBackground(null);
            if (!selectTextVal.equals("月")) {
                selectTextVal = "月";
                initListVal = "month";
                initList("month");
            }
        });

        selectYear.setClickedListener(component -> {
            statistic_showYear.setVisibility(Component.VISIBLE);
            statistic_showMonth.setVisibility(Component.HIDE);
            image.setVisibility(Component.VISIBLE);

            selectYear.setBackground(finalElement);
            selectMonth.setBackground(null);
            selectAll.setBackground(null);
            if (!selectTextVal.equals("年")) {
                selectTextVal = "年";
                initListVal = "year";
                initList("year");
            }
        });
        selectAll.setClickedListener(component -> {
            statistic_showYear.setVisibility(Component.HIDE);
            statistic_showMonth.setVisibility(Component.HIDE);
            image.setVisibility(Component.INVISIBLE);
            selectAll.setBackground(finalElement);
            selectYear.setBackground(null);
            selectMonth.setBackground(null);
            if (!selectTextVal.equals("总")) {
                selectTextVal = "总";
                initListVal = "all";
                initList("all");
            }
        });
    }

    public void initList(String type) {
        DirectionalLayout directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_statistic_info);
        directionalLayout.removeAllComponents();

        AccountOperate accountOperate = new AccountOperate();
        List<AccountInfo> accountInfos = new ArrayList<>();
        switch (type) {
            case "all":
                accountInfos = accountOperate.selectAll();
                break;
            case "month": {
                Text textYear = (Text) findComponentById(ResourceTable.Id_statistic_showYear);
                Text textMonth = (Text) findComponentById(ResourceTable.Id_statistic_showMonth);
                int year_val = Integer.parseInt(textYear.getText().replaceFirst(".$", ""));
                int month_val = Integer.parseInt(textMonth.getText().replaceFirst(".$", ""));
                System.out.println(year_val + " " + month_val);
                accountInfos = accountOperate.select(year_val, month_val);
                break;
            }
            case "year": {
                Text textYear = (Text) findComponentById(ResourceTable.Id_statistic_showYear);
                int year_val = Integer.parseInt(textYear.getText().replaceFirst(".$", ""));
                accountInfos = accountOperate.select(year_val);
                break;
            }
        }

        Map<String, Double> map = new HashMap<>();

        double propertyIncome = 0;
        double propertyOutcome = 0;

        for (AccountInfo accountInfo : accountInfos) {
            String come_type = accountInfo.getType();
            if (accountInfo.getMoney() < 0) {
                propertyOutcome -= accountInfo.getMoney();
            } else {
                propertyIncome += accountInfo.getMoney();
            }

            if (map.containsKey(come_type)) {
                map.put(accountInfo.getType(), accountInfo.getMoney() + map.get(come_type));
            } else {
                map.put(accountInfo.getType(), accountInfo.getMoney());
            }
        }

        Text text_outcome = (Text) findComponentById(ResourceTable.Id_statistic_outcome);
        Text text_income = (Text) findComponentById(ResourceTable.Id_statistic_income);
        Text text_all = (Text) findComponentById(ResourceTable.Id_statistic_all);

        text_outcome.setText(String.valueOf(propertyOutcome));
        text_income.setText(String.valueOf(propertyIncome));
        text_all.setText(String.valueOf(propertyIncome - propertyOutcome));


        for (String str : map.keySet()) {

            Component container = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_property_info, null, false);

            Text property_info_account = (Text) container.findComponentById(ResourceTable.Id_property_info_account);
            Text property_info_money = (Text) container.findComponentById(ResourceTable.Id_property_info_money);

            property_info_account.setText(str);
            property_info_money.setText(String.valueOf(map.get(str)));

            directionalLayout.addComponent(container);
        }

    }

    private void setImage() {
        PixelMap pixelMap = image.getPixelMap();
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = pixelMap.getImageInfo().pixelFormat;
        options.editable = true;
        options.size = new Size(70, 40);

        PixelMap pixelMapNew = PixelMap.create(pixelMap, options);
        image.setPixelMap(pixelMapNew);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
